package com.xiaohang.springstudy.chapter1.config;

import com.google.common.base.Predicate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Tag;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.time.LocalDate;

import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;

@Configuration
@EnableSwagger2
public class MySwagger2DocumentationConfiguration {

    @Bean
    public Docket petApi() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(doFilteringRules()).build().pathMapping("/")
                .directModelSubstitute(LocalDate.class, String.class)
                //
                .genericModelSubstitutes(ResponseEntity.class).useDefaultResponseMessages(false)
                .enableUrlTemplating(false).tags(new Tag("chapter1", "练习一"))//
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("SpringBootStudy", "https://blog.51cto.com/12012821",
                "aswift@126.com");
        return new ApiInfoBuilder()//
                .title("swift的Api")//
                .description("服务接口说明")//
                .license("Swift License Version 1.0")//
                .contact(contact)//
                .version("1.0")//
                .build();
    }

    /**
     * 设置过滤规则 这里的过滤规则支持正则匹配
     * 若有静态方法在此之前加载就会报集合相关的错误.
     *
     * @return
     */
    private Predicate<String> doFilteringRules() {
//        return or(regex("/api/v1/users.*"), regex("/users"));
        return or(regex("/api/v1/users.*"));
    }
}
